package com.project.web.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.project.common.constant.ShiroConstants;
import com.project.common.utils.str.Convert;
import com.project.user.service.IUserCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.user.domain.UserCart;
import com.project.web.controller.common.BaseController;
import com.project.common.base.AjaxResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 购物车 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/user/userCart")
public class UserCartController extends BaseController {
    private String prefix = "normal";

    @Autowired
    private IUserCartService userCartService;

    /**
     * 查看购物车
     *
     * @param mmap
     * @return
     */
    @GetMapping()
    public String list(ModelMap mmap) {
        List<UserCart> userCart = userCartService.selectUserCart(this.getUserId());
        mmap.put("userCarts", userCart);
        mmap.put("topTitle", "购物车");
        return prefix + "/cart";
    }

    /**
     * 新增保存购物车
     *
     * @param userCart
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserCart userCart, HttpServletRequest request) {
        userCart.setUserId(this.getUserId());
        AjaxResult ajaxResult = toAjax(userCartService.insertUserCart(userCart));
        ajaxResult.put("data", userCartService.countUserCart(this.getUserId()));
        request.getSession().setAttribute(ShiroConstants.CURRENT_CART_NUM, userCartService.countUserCart(this.getUserId()));
        return ajaxResult;
    }


    /**
     * 修改保存购物车
     *
     * @param userCart
     * @param request
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserCart userCart, HttpServletRequest request) {
        userCart.setUserId(this.getUserId());
        AjaxResult result = toAjax(userCartService.updateUserCart(userCart));
        request.getSession().setAttribute(ShiroConstants.CURRENT_CART_NUM, userCartService.countUserCart(this.getUserId()));
        return result;
    }

    /**
     * 删除购物车
     *
     * @param goodsId
     * @param request
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String goodsId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", this.getUserId());
        map.put("goodsIds", Convert.toStrArray(goodsId));
        AjaxResult result = toAjax(userCartService.deleteUserCartByIds(map));
        request.getSession().setAttribute(ShiroConstants.CURRENT_CART_NUM, userCartService.countUserCart(this.getUserId()));
        return result;
    }

}
